package cn.edu.zjgsu.ito.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.zjgsu.ito.service.UserWalletManageServiceI;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.UserInfomationShowVo;
import cn.edu.zjgsu.ito.vo.VouchersIdVo;
import cn.edu.zjgsu.ito.vo.VouchersShowVo;

@Controller
@RequestMapping("userWalletManageController")
public class UserWalletManageController {

	@Autowired
	private UserWalletManageServiceI userWalletManageService;
	
	/**
	 * 用户查询自己代金券总数接口
	 * @param session
	 * @return
	 */
	@RequestMapping(value="lookVouchersSize", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> lookVouchersSize(HttpSession session) {

		Map<String, Object> result = new HashMap<String, Object>();
		
		int userId = Integer.valueOf(session.getAttribute("userId").toString());
//		int userId = 1;
		int vouchersSum = userWalletManageService.lookVouchersSize(userId);
		
		result.put("msg", vouchersSum);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 用户查看自己的代金券详情
	 * @param session
	 * @return
	 */
	@RequestMapping(value="lookOwnVouchers", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> lookOwnVouchers(HttpSession session) {

		Map<String, Object> result = new HashMap<String, Object>();

		int userId = Integer.valueOf(session.getAttribute("userId").toString());
//		int userId = 1;
		
		List<VouchersShowVo> vouchersShowVos = userWalletManageService.lookOwnVouchers(userId);
		
		result.put("msg",vouchersShowVos);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 用户兑换代金券商城首页接口
	 * @param session
	 * @return
	 */
	@RequestMapping(value="exchangeVouchersStore", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> exchangeVouchersStore(HttpSession session) {
		
		Map<String, Object> result = new HashMap<String, Object>();

		int userId = Integer.valueOf(session.getAttribute("userId").toString());
//		int userId = 1;
		
		List<VouchersShowVo> vouchersShowVos = userWalletManageService.exchangeVouchersStore();
		UserInfomationShowVo userInfomationShowVo = userWalletManageService.userInfomation(userId);
		
		result.put("msg", vouchersShowVos);
		result.put("user",userInfomationShowVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	/**
	 * 用户兑换代金券接口
	 * @param vouchersIdVo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="exchangeVouchers", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> exchangeVouchers(VouchersIdVo vouchersIdVo,HttpSession session) {
		
		Map<String, Object> result = new HashMap<String, Object>();

		int userId = Integer.valueOf(session.getAttribute("userId").toString());
//		int userId = 1;
		
		int integralScore = userWalletManageService.exchangeVouchers(vouchersIdVo,userId);
		
		result.put("msg", integralScore);
		result.put("code", Constant.OK);
		return result;
	}
	
}
